﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStructs
{
    class LinkedList
    {

        private Node headNode;

      

        public bool addNode(Object o)
        {
            Node tmp = headNode;

            if (headNode == null) // means list is empty
            {

                Node newNode = new Node(o);

                newNode.setNextNode(null);

                headNode = newNode;

            }
            else                 // Node is not empty , so header node points to first element
            {


                Node newNode = new Node(o);

                
                newNode.setNextNode(headNode);

                headNode = newNode;

             }


            return true;

        }

        // Find if the Linked List contains the given object "o"
        public string  Contains(Object o)
        {
            string found = "NOT FOUND, by Looping";


            Node currentNode = headNode;

            if (currentNode == null)
                found = "NOT FOUND, List is NULL";
            else
            {

                while (currentNode != null)  // loop untill the headNode points to null
                {

                    
                    if (currentNode.getData() == o)
                    {
                        found = "YES, by Looping";
                        break;
                    }
                    else
                    {
                        currentNode = currentNode.getNextNode(); // Move to next node....
                       

                    }


                }


            }


            return found;

        }


        // Replace an object at a perticular position
        public void Replace(int position, Object o)
        {

            int count = 0;

            Node currentNode = headNode;

            if (currentNode != null)
            {
                // search 

                while (currentNode != null)
                {
                    count++;

                    if (count == position)
                    {
                        // now replace it ...
                        currentNode.setData(o);
                    }


                    currentNode = currentNode.getNextNode();
                                       

                }

            }


        }

        public void diplayList()
        {
            

            Node currentNode = headNode;

            while (currentNode != null)
            {

                
                Console.Write(currentNode.getData() + " ----> ");

                currentNode = currentNode.getNextNode();
            }

           
        } 

        public void Remove(Object o)
       {

                Node currentNode = headNode;
                Node previous = currentNode;


                while (currentNode != null)
                {

                   

                    if (currentNode.getData() == o)
                    {

                        currentNode = currentNode.getNextNode();
                        previous.setNextNode(currentNode);
                        currentNode = currentNode.getNextNode();

                    }
                    else
                    {
                        previous = currentNode;
                        currentNode = currentNode.getNextNode();


                    }

                }

       }

        public Object  RemoveAtPosition(int position)
        {

            bool status = false;
            Object temp;


            Node currentNode = headNode;

            for (int i = 1; i < (position - 1); i++)
            {

                currentNode = currentNode.getNextNode();

            }

            temp = currentNode.getNextNode().getData();

            currentNode.setNextNode(currentNode.getNextNode().getNextNode());


            return temp;



        }

    }
}
